package com.coocaa.ops.admin.v1.upload.util;


import com.coocaa.ops.admin.v1.upload.properties.UploadProperties;
import com.coocaa.ops.admin.v1.upload.enums.UploadCategoryEnums;
import com.coocaa.ops.admin.v1.upload.exception.FileCheckException;

/**
 * @author bijiahao
 * @date : 2019/2/15.
 * @description
 */
public final class FileExtNameValidator {

    private static final String ZIP = ".zip";
    private static final String APK = ".apk";

    private FileExtNameValidator() {

    }

    public static void valid(String extName, UploadCategoryEnums uploadCategoryEnums) {
        switch (uploadCategoryEnums) {
            case IMG:
                validImg(extName);
                break;
            case ZIP:
                validZip(extName);
                break;
            case APK:
                validApk(extName);
                break;
            default:
                throw new RuntimeException("没有对应的处理！");
        }
    }


    public static void validApk(String extName) {
        if (!APK.equals(extName.toLowerCase())) {
            throw new FileCheckException("Only .apk files can be uploaded");
        }
    }


    public static void validZip(String extName) {
        if (!ZIP.equals(extName.toLowerCase())) {
            throw new FileCheckException("Only .zip files can be uploaded");
        }
    }

    public static void validImg(String extName) {
        if (!UploadProperties.Constants.IMG_TYPE_LIMIT.contains(extName.toLowerCase())) {
            throw new FileCheckException("Only upload" + UploadProperties.Constants.IMG_TYPE_LIMIT + "files");
        }
    }
}
